var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp()
Page({
  data: {
    tag:'',
    keywrod: '',
    searchStatus: false,
    storeList: [],
    helpKeyword: [],
    historyKeyword: [],
    categoryFilter: false,
    currentSort: 'name',
    currentSortType: 'default',
    currentSortOrder: 'desc',
    filterCategory: [],
    defaultKeyword: {},
    hotKeyword: [],
    page: 1,
    limit: 20,
    categoryId: 0
  },
  //事件处理函数
  closeSearch: function() {
    wx.navigateBack()
  },
  clearKeyword: function() {
    this.setData({
      keyword: '',
      searchStatus: false
    });
  },
  onLoad: function(options) {
    let tag = options.tag;
    this.setData({
      tag:tag,
      keyword:'',
      page: 1,
      storeList: []
    });

    let that = this;
    util.request(api.StoreList, {
      tag:that.data.tag,
      keyword: that.data.keyword,
      page: that.data.page,
      limit: that.data.limit,
      sort: that.data.currentSort,
      order: that.data.currentSortOrder
    }).then(function(res) {
      if (res.errno === 0) {
        that.setData({
          searchStatus: true,
          storeList: res.data.list
        });
      }
    });
  },

  getStoreList: function() {
    let that = this;
    util.request(api.StoreList, {
      tag:that.data.tag,
      keyword: that.data.keyword,
      page: that.data.page,
      limit: that.data.limit,
      sort: that.data.currentSort,
      order: that.data.currentSortOrder
    }).then(function(res) {
      if (res.errno === 0) {
        that.setData({
          searchStatus: true,
          storeList: res.data.list
        });
      }
    });
  },
  onReady: function() {
    // this.getStoreList();
  },
  // getSearchKeyword() {
  //   let that = this;
  //   util.request(api.SearchIndex).then(function(res) {
  //     if (res.errno === 0) {
  //       that.setData({
  //         historyKeyword: res.data.historyKeywordList,
  //         defaultKeyword: res.data.defaultKeyword,
  //         hotKeyword: res.data.hotKeywordList
  //       });
  //     }
  //   });
  // },

  // inputChange: function(e) {
  //   this.setData({
  //     keyword: e.detail.value,
  //     searchStatus: false
  //   });

  //   if (e.detail.value) {
  //     this.getHelpKeyword();
  //   }
  // },
  // getHelpKeyword: function() {
  //   let that = this;
  //   util.request(api.SearchHelper, {
  //     keyword: that.data.keyword
  //   }).then(function(res) {
  //     if (res.errno === 0) {
  //       that.setData({
  //         helpKeyword: res.data
  //       });
  //     }
  //   });
  // },
  inputFocus: function() {
    this.setData({
      searchStatus: false,
      storeList: []
    });
    if (this.data.keyword) {
      this.getHelpKeyword();
    }
  },
  // clearHistory: function() {
  //   this.setData({
  //     historyKeyword: []
  //   })

  //   util.request(api.SearchClearHistory, {}, 'POST')
  //     .then(function(res) {
  //       console.log('清除成功');
  //     });
  // },
  
  // onKeywordTap: function(event) {
  //   this.getSearchResult(event.target.dataset.keyword);
  // },
  getSearchResult(keyword) {
    if (keyword === '') {
      keyword = this.data.defaultKeyword.keyword;
    }
    this.setData({
      tag:'',
      keyword: keyword,
      page: 1,
      categoryId: 0
    });
    this.getStoreList();
  },

  // selectCategory: function(event) {
  //   let currentIndex = event.target.dataset.categoryIndex;
  //   let filterCategory = this.data.filterCategory;
  //   let currentCategory = null;
  //   for (let key in filterCategory) {
  //     if (key == currentIndex) {
  //       filterCategory[key].selected = true;
  //       currentCategory = filterCategory[key];
  //     } else {
  //       filterCategory[key].selected = false;
  //     }
  //   }
  //   this.setData({
  //     filterCategory: filterCategory,
  //     categoryFilter: false,
  //     categoryId: currentCategory.id,
  //     page: 1,
  //     storeList: []
  //   });
  //   this.getStoreList();
  // },
  onKeywordConfirm(event) {
    this.getSearchResult(event.detail.value);
  }
})